package com.allianz.webapps.secondController;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.allianz.frame.utils.StringUtil;
import com.allianz.webapps.common.CustomerContextHolder;
import com.allianz.webapps.dbpojo.AzclPushUser;
import com.allianz.webapps.https.interfaces.type001.pojo.User;
import com.allianz.webapps.service.AzclPushUserService;

@Controller
@RequestMapping(value="/baiduPush")
public class BaiduPushUserController {
	
	@Autowired
	private AzclPushUserService userService;

	/**
	 * @param channelId
	 * @return
	 */
	@RequestMapping(value="/pushUser")
	@ResponseBody
	public Map<String,Object> SaveChannelId(String channelId,HttpSession session){
		Map<String,Object> map = new HashMap<String,Object>();
		User user = (User) session.getAttribute("username");
		if (!StringUtils.isEmpty(channelId)) {
			if (user != null) {
				AzclPushUser  pushUser = new AzclPushUser();
				if (!StringUtils.isEmpty(channelId)) {
					pushUser.setChannelId(channelId);
					pushUser.setDeviceType("iOS");
					pushUser.setUserId(user.getUserIntId());
					CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_NEWTOUCH);
					String u=userService.queryUser(user.getUserIntId());
					if (StringUtil.isNull(u)) {
						CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_NEWTOUCH);
						userService.addUser(pushUser);
					}else{
						CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_NEWTOUCH);
						userService.updateUser(user.getUserIntId(), channelId);
					}
				}
			}
		}
		map.put("status", "success");
		return  map;
	}
}
